﻿#include "GUIObjectColorMapDialog.h"
#include "ui_GUIObjectColorMapDialog.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"

#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKRunTimeSetting.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"

#include "PickDataProvider/PickedDataProvider.h"
#include "PickDataProvider/GUIPickInfo.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QColorDialog>
#include <QRegExpValidator>

namespace GUI
{
    GUIObjectColorMapDialog::GUIObjectColorMapDialog(QWidget* parent /*= nullptr*/) :
        GUIDialogBase(parent),
        _ui(new Ui::GUIObjectColorMapDialog)
    {
        _ui->setupUi(this);
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);
        //初始化界面
        _ui->lineEdit_MaterialFluids->setValidator(new QRegExpValidator(QRegExp("(^$)|(^#)|^#([0-9]|[a-f]|[A-F]){0,5}([0-9]|[a-f]|[A-F])$")));
        _ui->lineEdit_PhyProp_Beam->setValidator(new QRegExpValidator(QRegExp("(^$)|(^#)|^#([0-9]|[a-f]|[A-F]){0,5}([0-9]|[a-f]|[A-F])$")));
        _ui->lineEdit_PhyProp_General->setValidator(new QRegExpValidator(QRegExp("(^$)|(^#)|^#([0-9]|[a-f]|[A-F]){0,5}([0-9]|[a-f]|[A-F])$")));
        _ui->lineEdit_PhyProp_Ribbed->setValidator(new QRegExpValidator(QRegExp("(^$)|(^#)|^#([0-9]|[a-f]|[A-F]){0,5}([0-9]|[a-f]|[A-F])$")));
        _ui->lineEdit_PhyProp_Uniform->setValidator(new QRegExpValidator(QRegExp("(^$)|(^#)|^#([0-9]|[a-f]|[A-F]){0,5}([0-9]|[a-f]|[A-F])$")));
        _ui->lineEdit_PhyProp_FEProp->setValidator(new QRegExpValidator(QRegExp("(^$)|(^#)|^#([0-9]|[a-f]|[A-F]){0,5}([0-9]|[a-f]|[A-F])$")));
        this->init();
    }

    GUIObjectColorMapDialog::~GUIObjectColorMapDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUIObjectColorMapDialog::on_pushButton_OK_clicked()
    {
        this->setDataFromWidget();
        this->accept();
    }

    void GUIObjectColorMapDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void GUIObjectColorMapDialog::on_pushButton_Apply_clicked()
    {
        this->setDataFromWidget();
        //获取渲染数据
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper == nullptr)return;
        graphOper->updateAll(true);
    }

    void GUIObjectColorMapDialog::on_pushButton_Restore_clicked()
    {
        //获取软件设置
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        if (!settings) return;
        //获取默认颜色数据
        QString fluidsColor = settings->getValue<QColor>("OriFluidsMaterialColor").name();
        QString beamColor = settings->getValue<QColor>("OriBeamPhyPropColor").name();
        QString generalColor = settings->getValue<QColor>("OriUniformPhyPropColor").name();
        QString ribbedColor = settings->getValue<QColor>("OriGeneralPhyProp").name();
        QString uniformColor = settings->getValue<QColor>("OriRibbedPhyProp").name();
        QString feProp = settings->getValue<QColor>("OriFEProperty").name();
        //设置界面数据
        _ui->pushButton_PhyProp_Beam->setStyleSheet(QString("background-color: %1;").arg(beamColor));
        _ui->pushButton_PhyProp_General->setStyleSheet(QString("background-color: %1;").arg(generalColor));
        _ui->pushButton_PhyProp_Ribbed->setStyleSheet(QString("background-color: %1;").arg(ribbedColor));
        _ui->pushButton_PhyProp_Uniform->setStyleSheet(QString("background-color: %1;").arg(uniformColor));
        _ui->pushButton_MaterialFluids->setStyleSheet(QString("background-color: %1;").arg(fluidsColor));
        _ui->pushButton_PhyProp_FEProp->setStyleSheet(QString("background-color: %1;").arg(feProp));
        _ui->lineEdit_PhyProp_Beam->setText(beamColor);
        _ui->lineEdit_PhyProp_General->setText(generalColor);
        _ui->lineEdit_PhyProp_Ribbed->setText(ribbedColor);
        _ui->lineEdit_PhyProp_Uniform->setText(uniformColor);
        _ui->lineEdit_MaterialFluids->setText(fluidsColor);
        _ui->lineEdit_PhyProp_FEProp->setText(feProp);
    }

    void GUIObjectColorMapDialog::on_pushButton_PhyProp_Beam_clicked()
    {
        QColor colorOri(_ui->lineEdit_PhyProp_Beam->text());
        //初始化
        QColorDialog dialog(colorOri);
        dialog.setWindowTitle(tr("Select Color"));
        int result = dialog.exec();
        if (result != QDialog::Accepted) return;
        //设置颜色
        QString colorStr = dialog.currentColor().name();
        _ui->pushButton_PhyProp_Beam->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_Beam->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_pushButton_PhyProp_General_clicked()
    {
        QColor colorOri(_ui->lineEdit_PhyProp_General->text());
        //初始化
        QColorDialog dialog(colorOri);
        dialog.setWindowTitle(tr("Select Color"));
        int result = dialog.exec();
        if (result != QDialog::Accepted) return;
        //设置颜色
        QString colorStr = dialog.currentColor().name();
        _ui->pushButton_PhyProp_General->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_General->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_pushButton_PhyProp_Ribbed_clicked()
    {
        QColor colorOri(_ui->lineEdit_PhyProp_Ribbed->text());
        //初始化
        QColorDialog dialog(colorOri);
        dialog.setWindowTitle(tr("Select Color"));
        int result = dialog.exec();
        if (result != QDialog::Accepted) return;
        //设置颜色
        QString colorStr = dialog.currentColor().name();
        _ui->pushButton_PhyProp_Ribbed->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_Ribbed->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_pushButton_PhyProp_Uniform_clicked()
    {
        QColor colorOri(_ui->lineEdit_PhyProp_Uniform->text());
        //初始化
        QColorDialog dialog(colorOri);
        dialog.setWindowTitle(tr("Select Color"));
        int result = dialog.exec();
        if (result != QDialog::Accepted) return;
        //设置颜色
        QString colorStr = dialog.currentColor().name();
        _ui->pushButton_PhyProp_Uniform->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_Uniform->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_pushButton_MaterialFluids_clicked()
    {
        QColor colorOri(_ui->lineEdit_MaterialFluids->text());
        //初始化
        QColorDialog dialog(colorOri);
        dialog.setWindowTitle(tr("Select Color"));
        int result = dialog.exec();
        if (result != QDialog::Accepted) return;
        //设置颜色
        QString colorStr = dialog.currentColor().name();
        _ui->pushButton_MaterialFluids->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_MaterialFluids->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_pushButton_PhyProp_FEProp_clicked()
    {
        QColor colorOri(_ui->lineEdit_PhyProp_FEProp->text());
        //初始化
        QColorDialog dialog(colorOri);
        dialog.setWindowTitle(tr("Select Color"));
        int result = dialog.exec();
        if (result != QDialog::Accepted) return;
        //设置颜色
        QString colorStr = dialog.currentColor().name();
        _ui->pushButton_PhyProp_FEProp->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_FEProp->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_lineEdit_PhyProp_Beam_editingFinished()
    {
        QString colorStr = _ui->lineEdit_PhyProp_Beam->text();
        //处理输入数据
        if (!this->processColorData(colorStr, colorStr)) return;
        //设置颜色
        _ui->pushButton_PhyProp_Beam->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_Beam->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_lineEdit_PhyProp_General_editingFinished()
    {
        QString colorStr = _ui->lineEdit_PhyProp_General->text();
        //处理输入数据
        if (!this->processColorData(colorStr, colorStr)) return;
        //设置颜色
        _ui->pushButton_PhyProp_General->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_General->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_lineEdit_PhyProp_Ribbed_editingFinished()
    {
        QString colorStr = _ui->lineEdit_PhyProp_Ribbed->text();
        //处理输入数据
        if (!this->processColorData(colorStr, colorStr)) return;
        //设置颜色
        _ui->pushButton_PhyProp_Ribbed->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_Ribbed->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_lineEdit_PhyProp_Uniform_editingFinished()
    {
        QString colorStr = _ui->lineEdit_PhyProp_Uniform->text();
        //处理输入数据
        if (!this->processColorData(colorStr, colorStr)) return;
        //设置颜色
        _ui->pushButton_PhyProp_Uniform->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_Uniform->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_lineEdit_MaterialFluids_editingFinished()
    {
        QString colorStr = _ui->lineEdit_MaterialFluids->text();
        //处理输入数据
        if (!this->processColorData(colorStr, colorStr)) return;
        //设置颜色
        _ui->pushButton_MaterialFluids->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_MaterialFluids->setText(colorStr);
    }

    void GUIObjectColorMapDialog::on_lineEdit_PhyProp_FEProp_editingFinished()
    {
        QString colorStr = _ui->lineEdit_PhyProp_FEProp->text();
        //处理输入数据
        if (!this->processColorData(colorStr, colorStr)) return;
        //设置颜色
        _ui->pushButton_PhyProp_FEProp->setStyleSheet(QString("background-color: %1;").arg(colorStr));
        _ui->lineEdit_PhyProp_FEProp->setText(colorStr);
    }

    void GUIObjectColorMapDialog::init()
    {
        //获取软件设置
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        if (!settings) return;
        //从软件个性化配置获取颜色
        QVariant fluidsColor = settings->getVarient("FluidsMaterialColor");
        QVariant beamColor = settings->getVarient("BeamPhyPropColor");
        QVariant generalColor = settings->getVarient("UniformPhyPropColor");
        QVariant ribbedColor = settings->getVarient("GeneralPhyProp");
        QVariant uniformColor = settings->getVarient("RibbedPhyProp");
        QVariant fePropColor = settings->getVarient("FEProperty");
        //获取颜色数据
        if (beamColor.isNull()) beamColor = settings->getVarient("OriFluidsMaterialColor");
        if (generalColor.isNull()) generalColor = settings->getVarient("OriBeamPhyPropColor");
        if (ribbedColor.isNull()) ribbedColor = settings->getVarient("OriUniformPhyPropColor");
        if (uniformColor.isNull()) uniformColor = settings->getVarient("OriGeneralPhyProp");
        if (fluidsColor.isNull()) fluidsColor = settings->getVarient("OriRibbedPhyProp");
        if (fePropColor.isNull()) fePropColor = settings->getVarient("OriFEProperty");
        QString fluids = fluidsColor.value<QColor>().name();
        QString beam = beamColor.value<QColor>().name();
        QString general = generalColor.value<QColor>().name();
        QString ribbed = ribbedColor.value<QColor>().name();
        QString uniform = uniformColor.value<QColor>().name();
        QString feProp = fePropColor.value<QColor>().name();
        //设置界面
        _ui->pushButton_PhyProp_Beam->setStyleSheet(QString("background-color: %1;").arg(beam));
        _ui->pushButton_PhyProp_General->setStyleSheet(QString("background-color: %1;").arg(general));
        _ui->pushButton_PhyProp_Ribbed->setStyleSheet(QString("background-color: %1;").arg(ribbed));
        _ui->pushButton_PhyProp_Uniform->setStyleSheet(QString("background-color: %1;").arg(uniform));
        _ui->pushButton_MaterialFluids->setStyleSheet(QString("background-color: %1;").arg(fluids));
        _ui->pushButton_PhyProp_FEProp->setStyleSheet(QString("background-color: %1;").arg(feProp));
        _ui->lineEdit_PhyProp_Beam->setText(beam);
        _ui->lineEdit_PhyProp_General->setText(general);
        _ui->lineEdit_PhyProp_Ribbed->setText(ribbed);
        _ui->lineEdit_PhyProp_Uniform->setText(uniform);
        _ui->lineEdit_MaterialFluids->setText(fluids);
        _ui->lineEdit_PhyProp_FEProp->setText(feProp);
    }

    bool GUIObjectColorMapDialog::processColorData(QString colorOri, QString& colorStr)
    {
        colorStr.clear();
        if (colorOri.isEmpty())
            colorStr = "#000000";
        else if (colorOri.size() < 7)
            colorStr = colorOri + QString("%1").arg(0, 7 - colorOri.size(), 10, QLatin1Char('0'));
        else
            colorStr = colorOri;
        //是否匹配
        bool ok = QRegExp("^#([0-9]|[a-f]|[A-F]){5}([0-9]|[a-f]|[A-F])$").exactMatch(colorStr);
        return ok;
    }

    void GUIObjectColorMapDialog::setDataFromWidget()
    {
        //从界面获取数据
        QString fluids = _ui->lineEdit_MaterialFluids->text();
        QString beam = _ui->lineEdit_PhyProp_Beam->text();
        QString general = _ui->lineEdit_PhyProp_General->text();
        QString ribbed = _ui->lineEdit_PhyProp_Ribbed->text();
        QString uniform = _ui->lineEdit_PhyProp_Uniform->text();
        QString feProp = _ui->lineEdit_PhyProp_FEProp->text();
        //设置颜色
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        settings->setValue("FluidsMaterialColor", QColor(fluids));
        settings->setValue("BeamPhyPropColor", QColor(beam));
        settings->setValue("UniformPhyPropColor", QColor(uniform));
        settings->setValue("GeneralPhyProp", QColor(general));
        settings->setValue("RibbedPhyProp", QColor(ribbed));
        settings->setValue("FEProperty", QColor(feProp));
        //获取数据
        Acoustics::FITKAcousticsDiscModel* discModelData = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        Acoustics::FITKAcousticsPhysics* physicsData = FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
        if (!physicsData || !discModelData) return;
        Acoustics::FITKAcousticsFEModel* feModel = discModelData->getFEModel();
        if (!feModel) return;
        Interface::FITKAbstractSectionManager* sectionMgr = feModel->getSectionManager();
        Acoustics::FITKAcousticsMaterialManager* materialMgr = physicsData->getMaterialManager();
        Acoustics::FITKAcousticsPhysicsPropManager* physicsPropMgr = physicsData->getPhysicsPropManager();
        if (!materialMgr || !physicsPropMgr || !sectionMgr) return;
        //更新材料颜色
        int count = materialMgr->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Acoustics::FITKAcousticsAbsMaterial* material = dynamic_cast<Acoustics::FITKAcousticsAbsMaterial*>(materialMgr->getDataByIndex(i));
            if (!material) continue;
            material->updateDefaultColor();
        }
        //更新物理属性颜色
        count = physicsPropMgr->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Acoustics::FITKAcousticsAbsPhysicsProp* physicsProp = dynamic_cast<Acoustics::FITKAcousticsAbsPhysicsProp*>(physicsPropMgr->getDataByIndex(i));
            if (!physicsProp) continue;
            physicsProp->updateDefaultColor();
        }
        //更新FE属性
        count = sectionMgr->getDataCount();
        int sectionFlag = FITKAPP->getGlobalData()->getRunTimeSetting()->getValue("SECTIONCOLOR").toInt();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbstractSection* section = sectionMgr->getDataByIndex(i);
            if (!section) continue;
            section->setUserData(sectionFlag, QColor(feProp));
        }
    }
}

